#include "Drv_UART.h"


// printf Redirect
#if 1

#if !defined(__MICROLIB)
__asm (".global __use_no_semihosting\n\t");

void _sys_exit(int x) 
{
  x = x;
}

void _ttywrch(int ch)
{
  ch = ch;
}
#endif

#include "stdio.h"

FILE __stdout;
extern UART_HandleTypeDef huart1;

int fputc(int ch, FILE *f)
{
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 50);
  return ch;
}

#endif

// DMA buffer
#if 0

extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart2_rx;

#define RxBufSize (64)

uint8_t RxBuf[RxBufSize];
void receProcess(uint8_t *pData,uint16_t Count);

/* IRQ template */
// void UART5_IRQHandler(void)
// {
//   /* USER CODE BEGIN UART5_IRQn 0 */
// 	if(__HAL_UART_GET_FLAG(&huart5,UART_FLAG_IDLE))
// 	{		
// 		__HAL_UART_CLEAR_IDLEFLAG(&huart5);
// 		huart5.RxState = HAL_UART_STATE_READY;
// 		hdma_uart5_rx.State = HAL_DMA_STATE_READY;
//		
// 		HAL_UART_RxCpltCallback(&huart5);
// 	}
// 	else
//   /* USER CODE END UART5_IRQn 0 */
//   HAL_UART_IRQHandler(&huart5);
//   /* USER CODE BEGIN UART5_IRQn 1 */

//   /* USER CODE END UART5_IRQn 1 */
// }


// void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
// {
//  if(huart == &huart2)
//  {
//   static uint16_t count;

//   count = RxBufSize - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);
//   if(count == 0)
//     return;
//   hdma_usart2_rx.Lock = HAL_UNLOCKED;
//   if(huart->pRxBuffPtr < RxBuf[1])
//   {
//    myUARTRxCompleteCallback(RxBuf[0],&count);
//    HAL_UART_Receive_DMA(&huart2,RxBuf[1],RxBufSize);
//   }
//   else
//   {
//    myUARTRxCompleteCallback(RxBuf[1],&count);
//    HAL_UART_Receive_DMA(&huart2,RxBuf[0],RxBufSize);
//   }
//   hdma_usart2_rx.Lock = HAL_LOCKED;
//  }
// }

void UART_IDLE_Init(UART_HandleTypeDef *huart)
{
  __HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);
  HAL_UART_Receive_DMA(huart, RxBuf, RxBufSize);
}


void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{
  uint32_t recv_len;

  if (huart == &huart2)
  {
    HAL_UART_DMAStop(huart);

    recv_len = RxBufSize - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);
    if(recv_len == 0)
			return;
		else
    {
      receProcess(RxBuf, recv_len);
    }
    __HAL_UNLOCK(huart);
    HAL_UART_Receive_DMA(huart, RxBuf, RxBufSize);
  }
}


void receProcess(uint8_t *pData,uint16_t Count)
{
	if((pData[0] == 0x10) &&(Count == 13))
  {
    uint32_t fre_begin = 0;
    uint32_t fre_end = 0;
    uint32_t fre_step = 0;

    extern uint8_t flag_sweep;
    extern uint32_t gFreBegin;
    extern uint32_t gFreEnd;
    extern uint32_t gFreStep;

    fre_begin += (pData[1] << 24);
    fre_begin += (pData[2] << 16);
    fre_begin += (pData[3] <<  8);
    fre_begin += (pData[4] <<  0);

    fre_end += (pData[5] << 24);
    fre_end += (pData[6] << 16);
    fre_end += (pData[7] <<  8);
    fre_end += (pData[8] <<  0);

    fre_step += (pData[ 9] << 24);
    fre_step += (pData[10] << 16);
    fre_step += (pData[11] <<  8);
    fre_step += (pData[12] <<  0);

		// HAL_UART_Transmit_DMA(&huart2, (uint8_t*)"received\n", 9);
    gFreBegin = fre_begin;
    gFreEnd = fre_end;
    gFreStep = fre_step;
    flag_sweep = 1;
  }
  else if((pData[0] == 0x20) &&(Count == 1))
  {
    extern uint8_t flag_getLine;

    flag_getLine = 1;
  }
}

#endif